/**
 *
 * ZhangKongli Frame JS
 * Copyright 掌控力, inc. all rights reserved .
 * @author LiYunpeng<liyunpeng@live.com>
 * @constructor
 */
var ZFrame = {
	global : {
        //每间隔listenSessionDelay秒请求一次接口判断session是否超时(5分钟)
        listenSessionDelay:1000*5//*60
    },
    /**
     * 专门用户派发事件的对象
     */
    dispatcher : $({}),
    /**
     * 定义事件
     */
	events : {
	},
	/**
	 * 定义私有变量
	 */
	_var : {
	},
	
    /**
     * 依赖于JQuery的Ajax请求
     * @param obj JQuery.ajax方法的obj参数
     */
	ajax : function(obj) {
		var successFunc = obj.success;
		var newSuccessFunc = function(data, textStatus, jqXHR) {
			if(data.result==1) {
				//重置监听
				ZFrame.listenSessionTimeout();
			} else {
				if(ZFrame._var.sessionTimeoutCallback) {
					clearInterval(ZFrame._var.timer);
					ZFrame._var.sessionTimeoutCallback();
				}
			}
			if(successFunc) {
				successFunc(data, textStatus, jqXHR);
			}
		}
		obj.success = newSuccessFunc;
		//执行JQuery的Ajax
		$.ajax(obj);
	},
    /**
     * 监听Session超时,需与服务器端配合使用
     * @param url 判断Session是否超时的接口地址
     */
	listenSessionTimeout : function(url,delay,callback) {
		var that = this;
		if(ZFrame._var.timer) {
			clearInterval(ZFrame._var.timer);
		} else {
			ZFrame._var.checkSessionDelay = delay;
			ZFrame._var.checkSessionUrl = url;
			ZFrame._var.sessionTimeoutCallback = callback;
		}
		
		if(ZFrame._var.checkSessionUrl) {
			ZFrame._var.timer = setInterval(function() {
				that.ajax({url:ZFrame._var.checkSessionUrl,dataType:"json"});
			},ZFrame._var.checkSessionDelay*1000 + 60000);
		}
	},
	
	treeToList : function(treeData,eachCallBack) {
		var listData = [];
		if(treeData) {
			for(var i = 0;i<treeData.length;i++) {
				var eachData = treeData[i];
				listData.push(eachData);
				eachCallBack(eachData);
				listData.push(ZFrame.treeToList(eachData.children,eachCallBack));
			}
		}
		return listData;
	},
	
	eachTree : function(treeData,fn) {
		for(var i=0;i<treeData.length;i++) {
			var ret = fn(treeData[i]);
			if(ret == "return") {
				return ret;
			} else if (ret == "continue") {
				continue;
			} else if (ret == "break") {
				break;
			}
			if(treeData[i].children==undefined) {
				continue;
			} else {
				var eachRet = ZFrame.eachTree(treeData[i].children,fn);
				if(eachRet == "return") {
					return eachRet;
				}
			}
		}
	},
	getUUID:  function (len, radix) {
		var CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
	    var chars = CHARS, uuid = [], i;
	    radix = radix || chars.length;
	 
	    if (len) {
	      // Compact form
	      for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix];
	    } else {
	      // rfc4122, version 4 form
	      var r;
	 
	      // rfc4122 requires these characters
	      uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
	      uuid[14] = '4';
	 
	      // Fill in random data.  At i==19 set the high bits of clock sequence as
	      // per rfc4122, sec. 4.1.5
	      for (i = 0; i < 36; i++) {
	        if (!uuid[i]) {
	          r = 0 | Math.random()*16;
	          uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
	        }
	      }
	    }
	 
	    return uuid.join('');
	  },
	  /**
	   * 表单验证对象 
	   * 正则来源地址
	   * http://tools.netshiftmedia.com/regexlibrary/#
	   */
	validate :{
		custom:function(regex,$value){
			if($value){
				var patrn=regex; 
				if (!patrn.exec($value)) 
					return false ;
				return true;
			}else{
				return true;
			}
		},
		number:function($value){
			return this.custom(/^\d*[0-9](\.\d*[0-9])?$/,$value);
		},
		email:function($value){
			return this.custom(/^([0-9a-zA-Z]+([_.-]?[0-9a-zA-Z]+)*@[0-9a-zA-Z]+[0-9,a-z,A-Z,.,-]*(.){1}[a-zA-Z]{2,4})+$/,$value);
		}
	}
};